package com.store.mapper;

import com.store.entity.OrderItem;
import com.store.entity.Product;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface OrderItemMapper {
    @Select("SELECT * FROM order_item WHERE order_id = #{orderId}")
    List<OrderItem> findByOrderId(Long orderId);

    @Insert("INSERT INTO order_item (order_id, product_id, product_name, price, quantity, total_price) " +
            "VALUES (#{orderId}, #{productId}, #{productName}, #{price}, #{quantity}, #{totalPrice})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(OrderItem orderItem);

    @Select("SELECT p.*, SUM(oi.quantity) as total_quantity " +
            "FROM order_item oi " +
            "JOIN product p ON oi.product_id = p.id " +
            "JOIN `order` o ON oi.order_id = o.id " +
            "WHERE o.status = 'COMPLETED' " +
            "GROUP BY p.id " +
            "ORDER BY total_quantity DESC " +
            "LIMIT 1")
    Product findTopSellingProduct();
} 